﻿using System;
using Microsoft.Win32;

namespace Moonlite.ExtensionApi
{
    public static class RegistrySettings
    {
        private const string DefaultRegistryPath = "Software\\Vestras\\Moonlite";
        public static RegistryKey RegistryKey { get; set; }

        public static void Write(string key, object value)
        {
            RegistryKey.SetValue(key, value);
        }

        public static bool Exists(string key)
        {
            try
            {
                return Read(key) != null;
            }
            catch
            {
                return false;
            }
        }

        public static bool ExistsFromCurrentUserKey(string key)
        {
            try
            {
                return ReadFromCurrentUserKey(key) != null;
            }
            catch
            {
                return false;
            }
        }

        public static bool ExistsFromLocalMachineKey(string key)
        {
            try
            {
                return ReadFromLocalMachineKey(key) != null;
            }
            catch
            {
                return false;
            }
        }

        public static object Read(string key)
        {
            return RegistryKey.GetValue(key);
        }

        public static object ReadFromCurrentUserKey(string key)
        {
            string path = key.Substring(0, key.LastIndexOf("\\"));
            key = key.Substring(key.LastIndexOf("\\") + "\\".Length);
            OpenPath(path);
            return RegistryKey.GetValue(key);
        }

        public static object ReadFromLocalMachineKey(string key)
        {
            string path = key.Substring(0, key.LastIndexOf("\\"));
            key = key.Substring(key.LastIndexOf("\\") + "\\".Length);
            OpenPathFromLocalMachineKey(path);
            return RegistryKey.GetValue(key);
        }

        public static void CreateFolder(string key)
        {
            RegistryKey.CreateSubKey(key);
        }

        public static void OpenFolder(string key)
        {
            RegistryKey = RegistryKey.OpenSubKey(key, true);

            if (RegistryKey != null)
                return;

            CreateFolder(key);
            OpenFolder(key);
        }

        public static void OpenPathFromKey(RegistryKey registryKey, string path)
        {
            RegistryKey = registryKey;
            while (true)
            {
                bool shouldBreak = false;
                int slashIndex = path.IndexOf("\\");
                string key = "";
                if (slashIndex == -1)
                {
                    key = path;
                    path = "";
                    shouldBreak = true;
                }
                else
                {
                    key = path.Substring(0, slashIndex);
                    path = path.Substring(slashIndex + "\\".Length);
                }

                if (key != "")
                {
                    RegistryKey = RegistryKey.OpenSubKey(key, true);
                }

                if (shouldBreak == true)
                {
                    break;
                }
            }
        }

        public static void OpenPath(string path)
        {
            OpenPathFromKey(Registry.CurrentUser, path);
        }

        public static void OpenPathFromLocalMachineKey(string path)
        {
            OpenPathFromKey(Registry.LocalMachine, path);
        }

        public static void CloseFolder()
        {
            RegistryKey = Registry.CurrentUser.OpenSubKey(DefaultRegistryPath);
        }

        public static void Initialize()
        {
            RegistryKey currentUser = Registry.CurrentUser;
            RegistryKey = currentUser.OpenSubKey(DefaultRegistryPath, true);

            if (RegistryKey == null)
            {
                RegistryKey = currentUser.CreateSubKey(DefaultRegistryPath);
            }
        }
    }
}
